package com.base.utils.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import com.base.common.cfg.PropertiesHelper;
import com.base.common.file.FileCommon;
import com.base.utils.helper.CommonHelper;

public class HttpProperties {
	private static String PROPERTIES_FILE_NAME = "cfg/http_client.properties";
	public static PropertiesHelper props;

	public static String getPROPERTIES_FILE_NAME() {
		return PROPERTIES_FILE_NAME;
	}

	public static void reloadProperties(String name) {
		if (name != null)
			PROPERTIES_FILE_NAME = name;
		props = null;
		loadProperties();
	}

	private static void loadProperties() {
		try {
			System.out.println("Load [generator.properties] from classpath");
			props = new PropertiesHelper(loadAllPropertiesByClassLoader(PROPERTIES_FILE_NAME));

			for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
				System.out.println("[Property] " + entry.getKey() + "=" + entry.getValue());
			}

			System.out.println();
		} catch (IOException e) {
			throw new RuntimeException("Load Properties error", e);
		}
	}

	public static Properties getProperties() {
		return getHelper().getProperties();
	}

	private static PropertiesHelper getHelper() {
		if (props == null)
			loadProperties();
		return props;
	}

	public static String getProperty(String key, String defaultValue) {
		return getHelper().getProperty(key, defaultValue);
	}

	public static String getProperty(String key) {
		return getHelper().getProperty(key);
	}

	public static String getRequiredProperty(String key) {
		return getHelper().getRequiredProperty(key);
	}

	public static int getRequiredInt(String key) {
		return getHelper().getRequiredInt(key);
	}

	public static boolean getRequiredBoolean(String key) {
		return getHelper().getRequiredBoolean(key);
	}

	public static String getNullIfBlank(String key) {
		return getHelper().getNullIfBlank(key);
	}

	public static void setProperty(String key, String value) {
		getHelper().setProperty(key, value);
	}

	public static void setProperties(Properties v) {
		props = new PropertiesHelper(v);
	}

	public static Properties loadAllPropertiesByClassLoader(String resourceName) throws IOException {
		Properties properties = new Properties();

		FileCommon.mkdirWithFullName(CommonHelper.Path.getConfigFull() + PROPERTIES_FILE_NAME);
		properties = PropertiesHelper.loadByFilePath(CommonHelper.Path.getConfigFull() + PROPERTIES_FILE_NAME);
		System.out.println("===========properties:" + properties + " file:" + CommonHelper.Path.getConfigFull()
				+ PROPERTIES_FILE_NAME + "");
		if (properties != null) {
			return properties;
		}
		properties = new Properties();

		System.out.println("=========== " + CommonHelper.Path.getConfigFull() + PROPERTIES_FILE_NAME + " is null");
		Enumeration urls = HttpProperties.class.getClassLoader().getResources(resourceName);

		while (urls.hasMoreElements()) {
			URL url = (URL) urls.nextElement();
			InputStream input = null;
			try {
				URLConnection con = url.openConnection();
				con.setUseCaches(false);
				input = con.getInputStream();
				properties.load(input);
			} finally {
				if (input != null) {
					input.close();
				}
			}
		}
		return properties;
	}
}
